import React from 'react';
import {View, Image} from 'react-native';
import { Lt, Color, Label, Images, NavTheme as Nav, Jx, Flist, Fimage, Dialog, Jpp } from "./_utils";
import { Net, NET_NT } from "./_net"
import { ProjectorLR } from "./_util"

class ViewMsg extends React.PureComponent{
    render(){
        const {source, uri, title, date, children} = this.props
        return <View style = {{backgroundColor:"white", flexDirection:"row", padding:Lt.pad, marginBottom:Lt.margin}}>
            {
                source ? <Image source = {source} /> : 
                <Fimage style={{width:40, height:40, borderRadius:20,}} uri={uri}/>
            }
            <View style = {{marginLeft:Lt.margin, flexShrink:1}}>
                <Label style = {{fontSize:Lt.c16, color:Color.c3}}>{title}</Label>
                {
                    date ? 
                    <Label style = {{color:Color.c9, marginVertical:5}}>{Jx.toDate(date)}</Label>
                    : null
                }
                {
                    Jx.isString(children) ?
                    <Label style = {{color:Color.c6}}>
                        {/* <Label style = {{opacity:0}}>缩进</Label> */}
                        {children}
                    </Label> : children
                }
            </View>
        </View>
    }
}
const IDX_OFFICIAL = 0, IDX_ORDER = 1;
const c_images = [Images.logo_tg, Images.xiaoxi_da];
export class PageMsg extends React.PureComponent{
    static navigationOptions = {
        tabBarIcon : ({focused, tintColor})=><Image source={focused ? Images.xiaoxi_2 : Images.xiaoxi}/>
    }
    // 官方消息 订单消息
    onLoad(idx, params){
        let req = (idx === IDX_OFFICIAL) ? 
            Net.request("advice/adv", params.pagination, {NET_NT, params}) : 
            Net.request("other/oderAdvice", params.pagination, {params});
        req.then(r=>{
            if(r.data){
                params.add(r.data.pageData);
            }else{
                params.add()
            }
        });
    }
    onItem(idx, params){
        const item = params.item;
        if(idx === IDX_OFFICIAL){
            return <ViewMsg title={item.title} uri={item.image} date={item.createtime * 1000}>{item.content}</ViewMsg>
        }
        style = {color:Color.c6, marginTop:3};
        return <ViewMsg title={item.name} source={c_images[idx]}>
            <Label style={style}>订单单号: {item.oderSn}</Label>
            <Label style={style}>订单来源: {item.src_Name}</Label>
            <Label style={style}>下单用户: {item.userName}</Label>
            <Label style={style}>订单时间: {Jx.toDate(item.oder_Createtime)}</Label>
        </ViewMsg>
    }
    render(){
        return <View style = {{flex:1, backgroundColor:Color.back}}>
            <Nav.StateBar />
            <ProjectorLR fields = {["官方通知","订单通知"]} barBorderStyle={{backgroundColor:Color.theme}}>
                <Flist
                    onLoad={params=>this.onLoad(0, params)} 
                    onItem={params=>{return this.onItem(0, params);}}
                />
                <Flist
                    onLoad={params=>this.onLoad(1, params)} 
                    onItem={params=>{return this.onItem(1, params);}}
                />
            </ProjectorLR>
        </View>;
    }
}
